package com.example.springboottest.example.hutool;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.StreamProgress;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

public class HuToolIoTest {
    public static void main(String[] args) {
        ioTest();
        new HuToolIoTest().nioTest();
    }

    public static void ioTest() {
        try (BufferedInputStream in = FileUtil.getInputStream("/Users/fangdaye/Downloads/ESB转发SRM接口例子.xml"); BufferedOutputStream out = FileUtil.getOutputStream("/Users/fangdaye/Downloads/test.xml");) {
            long copySize = IoUtil.copy(in, out, IoUtil.DEFAULT_BUFFER_SIZE);
            System.out.println(copySize);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void nioTest() {
        StreamProgress progress = new HuToolIoTest.StreamProgressImpl();

        try (BufferedInputStream in = FileUtil.getInputStream("/Users/fangdaye/soft/eclipse-inst-jre-mac64.dmg");
             BufferedOutputStream out = FileUtil.getOutputStream("/Users/fangdaye/Downloads/test.dmg")) {
            long copySize = IoUtil.copyByNIO(in, out, IoUtil.DEFAULT_BUFFER_SIZE, progress);
            System.out.println(copySize);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public class StreamProgressImpl implements StreamProgress {

        @Override
        public void start() {
            System.out.println("start...");
        }

        @Override
        public void progress(long progressSize) {
            System.out.println("progress..." + progressSize);
        }

        @Override
        public void finish() {
            System.out.println("finish...");
        }
    }
}
